package com.example.mydata.Mapper;


import com.example.mydata.Vo.Book;
import com.example.mydata.Vo.BookStatus;
import com.example.mydata.Vo.Borrowes;
import com.example.mydata.Vo.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface Usermapper {

    // 根据ID查询书籍
    @Select("SELECT * FROM user_book WHERE id = #{id}")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "author", column = "author"),
            @Result(property = "stock", column = "stock"),
            @Result(property = "status", column = "status", javaType = BookStatus.class)
    })
    Book selectBookById(String id);

    // 查询所有书籍
    @Select("SELECT * FROM user_book")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "title", column = "title"),
            @Result(property = "author", column = "author"),
            @Result(property = "stock", column = "stock"),
            @Result(property = "status", column = "status", javaType = BookStatus.class)
    })
    List<Book> selectAllBooks();

    // 更新书籍库存
    @Update("UPDATE book SET stock = #{stock} WHERE id = #{id}")
    void updateBookStock(@Param("id") String id, @Param("stock") int stock);

    // 更新书籍状态
    @Update("UPDATE book SET status = #{status} WHERE id = #{id}")
    void updateBookStatus(@Param("id") String id, @Param("status") BookStatus status);


    // 插入用户
    @Insert("INSERT INTO user_user (user_id, name) VALUES (#{userId}, #{name})")
    void insertUser(User user);

    // 根据ID查询用户
    @Select("SELECT * FROM user_user WHERE user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "borrowedBooks", column = "user_id",
                    many = @Many(select = "selectBorrowedBooksByUserId"))
    })
    User selectUserById(String userId);

    // 查询所有用户
    @Select("SELECT * FROM user_user")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "borrowedBooks", column = "user_id",
                    many = @Many(select = "selectBorrowedBooksByUserId"))
    })
    List<User> selectAllUsers();


    // 插入借书记录
    @Insert("INSERT INTO borrowed_book (user_id, book_id, borrow_date) " +
            "VALUES (#{userId}, #{bookId}, #{borrowDate})")
    void insertBorrowedBook(Borrowes borrowes);

    // 根据用户ID查询借书记录
    @Select("SELECT * FROM borrowed_book WHERE user_id = #{userId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "bookId", column = "book_id"),
            @Result(property = "borrowDate", column = "borrow_date"),
            @Result(property = "returnDate", column = "return_data")
    })
    List<BookStatus> selectBorrowedBooksByUserId(String userId);


    //查询书籍信息
    @Select(" SELECT * FROM borrowed_book WHERE user_id = #{userId} AND book_id =#{bookId} and return_data is NULL")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "bookId", column = "book_id"),
            @Result(property = "borrowDate", column = "borrow_date"),
            @Result(property = "returnDate", column = "return_data")
    })
    Borrowes selectreturn(String userId,String bookId);
    @Select(" SELECT count(*) FROM borrowed_book WHERE user_id = #{userId} and return_data is NULL")
    int selectUnReturnCount(String userId);
    @Update("UPDATE borrowed_book SET return_data = #{return_data} WHERE id=#{id}")
    int updateTime(Borrowes borrowes);


    // 根据书籍ID查询借书记录
    @Select("SELECT * FROM borrowed_book WHERE book_id = #{bookId}")
    @Results({
            @Result(property = "userId", column = "user_id"),
            @Result(property = "bookId", column = "book_id"),
            @Result(property = "borrowDate", column = "borrow_date"),
            @Result(property = "returnDate", column = "return_data")
    })
    List<Borrowes> selectBorrowedBooksByBookId(String bookId);

    // 更新还书日期
    @Update("UPDATE borrowed_book SET return_data = #{returnDate} " +
            "WHERE user_id = #{userId} AND book_id = #{bookId}")
    void updateReturnDate(@Param("userId") String userId,
                          @Param("bookId") String bookId,
                          @Param("returnDate") String returnDate);

    // 删除借书记录
    @Delete("DELETE FROM borrowed_book WHERE user_id = #{userId} AND book_id = #{bookId}")
    void deleteBorrowedBook(@Param("userId") String userId, @Param("bookId") String bookId);
}
